package com.example.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.BookService.impl.BookService;
import com.example.demo.commom.Result;
import com.example.demo.entity.Book;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 书籍管理
 */
@RestController
@RequestMapping("/api/book")
@Slf4j
public class BookController {

    @Resource
    BookService bookService;

    /**
     * 上架图书
     *
     * @param Book
     * @return
     */
    @PostMapping
    public Result<?> save(@RequestBody Book Book) {
        log.info("上架图书：{}", Book);
        bookService.insert(Book);
        return Result.success();
    }

    /**
     * 修改图书
     *
     * @param Book
     * @return
     */
    @PutMapping
    public Result<?> update(@RequestBody Book Book) {
        log.info("修改图书：{}", Book);
        bookService.updateById(Book);
        return Result.success();
    }

    /**
     * 根据书籍id批量删除
     *
     * @param ids
     * @return
     */
    @PostMapping("/deleteBatch")
    public Result<?> deleteBatch(@RequestBody List<Integer> ids) {
        log.info("批量删除图书：{}", ids);
        bookService.deleteBatchIds(ids);
        return Result.success();
    }

    /**
     * 根据书籍id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        log.info("根据书籍id删除：{}", id);
        bookService.deleteById(id);
        return Result.success();
    }

    /**
     * 分页查询书籍
     *
     * @param pageNum  页码数
     * @param pageSize 每页展示数
     * @param search1  书籍id
     * @param search2  书名
     * @param search3  书作者
     * @return
     */
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search1,
                              @RequestParam(defaultValue = "") String search2,
                              @RequestParam(defaultValue = "") String search3) {
        log.info("分页查询图书：页码：{},页数：{},图书编号：{},图书名称：{},作者：{}", pageNum, pageSize, search1, search2, search3);
        Page<Book> BookPage = bookService.selectPage(pageNum, pageSize, search1, search2, search3);
        return Result.success(BookPage);
    }
}
